#include "Follower.h"
#include "Renderer.h"
#include <new>
#include <math.h>

Follower::Follower(int x, int y)	
	:
	GameObject(OBJECT_TYPE::FOLLOWER),
	m_destination(),
	m_speed(4)
{
	Position& pos = GetPosition();
	pos.x = x;
	pos.y = y;
	pos.height = 10;
	pos.width = 10;
}

Follower::Follower(long long objID, Position& pos)
	:
	GameObject(OBJECT_TYPE::FOLLOWER),
	m_destination(),
	m_speed(4)
{
	m_objectID = objID;
	Position& myPos = GetPosition();
	myPos.x = pos.x;
	myPos.y = pos.y;
	myPos.height = pos.height;
	myPos.width = pos.width;
}

Follower::~Follower(void)
{

}

void Follower::SetTarget(int x, int y)
{
	m_destination.x = x;
	m_destination.y = y;
}

void Follower::DestroyData(void)
{

}
void Follower::Copy(void* address)
{
	new (address)Follower(*this);

}
void Follower::Update(void)
{
	if (m_destination.x != 0 && m_destination.y != 0)
	{
		Position& pos = GetPosition();
		if (pos.x == m_destination.x && pos.y == m_destination.y)
			return;

		float directionX = m_destination.x - pos.x;
		float directionY = m_destination.y - pos.y;
	
		int length = sqrt(static_cast<float>(directionX * directionX + directionY * directionY));
		
		directionX /= length;
		directionY /= length;

		pos.x += directionX * m_speed;
		pos.y += directionY * m_speed;
	}
}
void Follower::Draw(void)
{
	Renderer::GetInstance().DrawObject(*this);
}
int Follower::GetObjSize(void)
{
	return sizeof(*this);
}
SDL_Texture* Follower::GetTexture(void)
{
	static SDL_Texture* texture = 0;
	if (!texture)
	{
		texture = Renderer::GetInstance().LoadTexture("c:/nerd.bmp");
	}
	return texture;
}